package com.wxg.junit.appendix.B;

/**
 * @author Wxg
 */
public class SampleTimingInterceptor implements Interceptor {

    Timer timer = new Timer();

    @Override
    public void interceptBefore() {
        System.out.println("Interceptor started.");
        timer.start();
    }

    @Override
    public void interceptAfter() {
        timer.stop();
        System.out.println("Interceptor ended."
                + "The test executed for " + timer.time());
    }

    class Timer {
        private long nanoStart = 1;
        private long nanoEnd = 0;

        void start() {
            nanoStart = System.nanoTime();
        }

        void stop() {
            nanoEnd = System.nanoTime();
        }

        long time() {
            return nanoEnd - nanoStart;
        }
    }
}
